﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Simply.BusinessTier.Facade.Interface;

using Simply.DataTier.Repository.Interface;
using Domain;
using Simply.DataTier.Repository.Implementation;

namespace Simply.BusinessTier.Facade.Implementation
{
    public class BillingService : IBillingService
    {
        //private IProductRepository productRepository;
        private IBillingRepository billingRepository;

        public BillingService(IBillingRepository billingRepository)
        {
            //this.productRepository = productRepository;
            this.billingRepository = billingRepository;
        }

        public BillingService()
            : this( new BillingEFRepository())
        {
        }

        public decimal GetDefaultConfigPrice(long productId, DateTime date)
        {
            ProductDO product = new ProductService().GetProductById(productId);
            return 0;
        }

        public decimal GetProductPrice(long productId, string mediaCode, string methodCode, 
                                    int quantity, DateTime date, string[] finishingCode)
        {
            
            string productType = new ProductService().GetProductById(productId).TypeKey;
            
            PricePolicyDO productPrice = billingRepository.GetProductMinPricePolicy(productId, quantity, date);
            PrintPricePolicyDO printPrice = billingRepository.GetPrintMinPricePolicy(productType, mediaCode, methodCode, quantity, date);
            decimal finishingPrice = 0;
            //List<FinishingPricePolicy> finishings = new List<FinishingPricePolicy>();
            if (finishingCode != null && finishingCode.Length != 0)
            {
                foreach (var fCode in finishingCode)
                {
                    FinishingPricePolicyDO fPrice = billingRepository.GetFinishingMinPricePolicy(productType, fCode, mediaCode, quantity, date);
                    finishingPrice += fPrice.Price;
                    //finishings.Add(fPrice);
                }
            }

            return productPrice.Price + printPrice.Price + finishingPrice;
        }
    }
}
